package com.company.dao;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.company.exception.DAOException;

public abstract class AbstractDaoHib<T> {
	private SessionFactory sessionFactory;

	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	
	
	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}	
	
	protected Object save(Object object) throws DAOException {
		getSession().saveOrUpdate(object);
		
		return object; 
	}
	
	protected void delete(Object object) throws DAOException {
		getSession().delete(object);
	}
	
	protected Object find(Class<T> clazz, long id) throws DAOException {
		return getSession().load(clazz, id);
	}
}
